package com.itheima.sys.controller;

import com.itheima.sys.pojo.Article;
import com.itheima.sys.pojo.PageBean;
import com.itheima.exception.model.Result;
import com.itheima.sys.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/article")
public class ArticleController {


    @Autowired
    private ArticleService articleService;

    @PostMapping
    public Result add(@RequestBody @Validated Article article) {
        articleService.add(article);
        System.out.println("2222");

        return Result.success();
    }

    @GetMapping
    public Result<PageBean<Article>> list(
            Integer pageNum,
            Integer pageSize,
            @RequestParam(required = false) Integer categoryId,
            @RequestParam(required = false) String state
    ) {
       PageBean<Article> pb =  articleService.list(pageNum,pageSize,categoryId,state);
       return Result.success(pb);
    }
    @DeleteMapping
    public Result<String> deleteArticle( Integer id) {
        articleService.getById(id);
        return Result.success("删除");
    }
    @PutMapping("/update")
    public Result<String> update(@RequestBody Article article) {
        articleService.update(article);
        return Result.success("修改");
    }
}
